import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/modle/play/bloc/yaoqing_bloc.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/route_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';

class YaoQingPage extends StatefulWidget {
  YaoQingPage({Key key}) : super(key: key);

  @override
  _YaoQingPageState createState() => _YaoQingPageState();
}

class _YaoQingPageState extends State<YaoQingPage> {
  YaoQingBloc _bloc;
  Uint8List bytes;
  @override
  void initState() {
    super.initState();
    _bloc = YaoQingBloc();
    _bloc.initDate.add(null);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Container(
        height: MediaQuery.of(context).size.height,
        decoration: new BoxDecoration(
          color: Colours.gray_f0,
        ),
        child: Stack(
          children: <Widget>[
            FadeInImage.assetNetwork(
              image:
                  'https://lizhengyouxuan.oss-cn-shanghai.aliyuncs.com/lizhengyouxuan/pingtuan/lizensenhuo_install.png',
              placeholder: 'images/picture_loading.png',
              height: MediaQuery.of(context).size.height,
              fit: BoxFit.fitHeight,
            ),
            //  WidgetUtils.getImageWidget('fenxiang', fit: BoxFit.fill),
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Expanded(child: Container()),
                Container(
                  height: MediaQuery.of(context).size.height / 4,
                  decoration: BoxDecoration(
                    color: Colours.transparent_25,
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      StreamBuilder(
                        stream: _bloc.sendDate,
                        initialData: bytes,
                        builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
                          Object object = snapshot.data;
                          if (object is String) {
                            bytes = Base64Decoder().convert(object);
                          }
                          return Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 32),
                            child: bytes != null
                                ? Image.memory(
                                    bytes,
                                    fit: BoxFit.fill,
                                  )
                                : Container(),
                          );
                        },
                      ),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Row(
                            children: <Widget>[
                              Icon(
                                Icons.account_circle,
                                size: 64,
                                color: Colors.white70,
                              ),
                              SizedBox(
                                width: 4,
                              ),
                              WidgetUtils.getTextColorView(
                                  //'ID:${BaseConstant.userId.substring(0, 4)}****${BaseConstant.userId.substring(BaseConstant.userId.length - 4, BaseConstant.userId.length)}',
                                  'ID:${BaseConstant.code.substring(0, 3)}****${BaseConstant.code.substring(BaseConstant.code.length - 4, BaseConstant.code.length)}',
                                  Colours.title_white,
                                  Dimens.FONT_SIZE_16,
                                  overflow: TextOverflow.ellipsis),
                            ],
                          ),
                          SizedBox(
                            height: 4,
                          ),
                          WidgetUtils.getTextColorView('邀请您参加推广', Colours.title_white, Dimens.FONT_SIZE_14,
                              overflow: TextOverflow.ellipsis),
                          SizedBox(
                            height: 4,
                          ),
                          WidgetUtils.getTextColorView('扫描下载APP', Colours.title_white, Dimens.FONT_SIZE_16,
                              overflow: TextOverflow.ellipsis),
                          SizedBox(
                            height: 4,
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
            _closeView(),
          ],
        ),
      ),
    );
  }

  Widget _closeView() {
    return GestureDetector(
      onTap: () {
        RouteUtil.closeView();
      },
      child: Container(
        width: 32,
        height: 32,
        margin: const EdgeInsets.symmetric(vertical: 32, horizontal: 16),
        decoration: BoxDecoration(
            border: Border.all(color: Colours.transparent_80, width: 1.0),
            borderRadius: BorderRadius.circular(60.0),
            color: Colours.transparent_80),
        child: Icon(
          Icons.close,
          color: Colours.title_white,
          size: 16,
        ),
      ),
    );
  }

  @override
  void dispose() {
    bytes = null;
    super.dispose();
  }
}
